
import java.io.*;
import java.util.ArrayList;

public class Idea implements Serializable {
    private static int id_counter = 1;
    private int id;
    private double value;
    private String name;
    private String description;
    private ArrayList<User> owners = new ArrayList<>();
    private ArrayList<Double> shares = new ArrayList<>();
    private ArrayList<Double> prices = new ArrayList<>();
    private ArrayList<Topic> topics = new ArrayList<>();

    public Idea() {
        this.id = id_counter;
        id_counter++;
        this.value = 100;
        this.name = "Empty idea";
        this.description = "Without description";
    }

    public Idea(double v, String n, String d, User u) {
        this.id = id_counter;
        id_counter++;
        this.value = v;
        this.name = n;
        this.description = d;
        this.owners.add(u);
        this.shares.add(100.0);
        this.prices.add(v);
    }

    public int getId(){
        return this.id;
    }

    public double getValue(){
        return this.value;
    }

    public String getName(){
        return this.name;
    }

    public String getDescription(){
        return this.description;
    }

    public ArrayList<User> getOwners(){
        return this.owners;
    }

    public ArrayList<Topic> getTopics(){
        return this.topics;
    }

    public void setName(String n){
        this.name = n;
    }

    public void setDescription(String n){
        this.description = n;
    }

    public void addOwner(User u, double share, double price){
        this.owners.add(u);
        this.shares.add(share);
        this.prices.add(price);
    }

    public void deleteOwner(User u){
        int pos = this.owners.indexOf(u);
        this.owners.remove(pos);
        this.shares.remove(pos);
        this.prices.remove(pos);
    }

    public void updateOwnerShare(User u, double s){
        int pos = this.owners.indexOf(u);
        this.shares.set(pos, s);
    }

    public void setPrice(double v, User u){
        int pos = this.owners.indexOf(u);
        if(pos!=-1){
            this.prices.set(pos, v);
        }
    }

    public double calcValue(){
        double total = 0;
        for(int i=0; i<this.prices.size(); i++){
            total += this.prices.get(i);
        }
        return total;
    }

    public void addTopic(Topic t){
        this.topics.add(t);
    }

    public void deleteTopic(Topic t){
        this.topics.remove(t);
    }

    @Override
    public String toString() {
        String i = String.valueOf(this.id);
        String v = String.valueOf(this.value);
        return "ID= " + i + ", Value= " + v + ", Idea= " + this.name;
    }
}
